/**
 * @Author: 耿立强
 * @Date: 2020/12/21
 * @Last Modified time: 2020/12/21 11:02:35
 */

 import { byAllTranslate, getAllUser, queryUnitCode, getRegion, getOrgList } from '@/api/repair/batchEventManagement/activityDistribution';

 import { getAllBrand, getAllSeries, getAllModel } from '@/api/repair/batchEventManagement/activityExecutionStatusQueryJingxsDuan';
 import { getNameByIds } from '@/api/repair/common/index.js';

 import { arrayObjDeWeight } from '@/utils';

 import { exportExcelFile } from '@/utils/index';

 import { isEmpty } from '@/utils/utils';
 import moment from 'moment';
 import Vue from 'vue';

 let v = new Vue();

 export const dealerMixin = {
   beforeCreate() {},
   methods: {
     async downHandleFile(fn, params, fileName) {
       await fn(params)
         .then(res => {
           console.log('params--', res.data);

           // let reader = new FileReader();
           // reader.addEventListener("loadend", function () { //
           //   let res = JSON.parse(reader.result); // 返回的数据
           //   console.log(res,'返回结果数据')
           // });
           // reader.readAsText(res.data, 'utf-8');

           // if (res.resultCode == 200 && res.success)
           // {
           exportExcelFile(res.data, fileName);
           // } else
           // {
           //   let errMsg = isEmpty(res.errMsg) ? '导出失败' : res.errMsg
           //   this.$notify.warning({title: '提示信息', message: errMsg});
           // }
         })
         .catch(err => {});
     },

     /*
      * 获取所有经销商
      *   columns 要搜索的数组
      *   dataField 匹配的属性
      *   type form  表单 table 表格
      * */
     async getAllDelears(columns, dataField, type = 'table') {
       let fieldList = [];
       if (!Array.isArray(dataField)) {
         fieldList = [dataField];
       } else {
         fieldList = dataField;
       }

       let res = await byAllTranslate();

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;
         //去除重复数据
         dataList = arrayObjDeWeight(dataList, 'dealerCode');
         let tempList = dataList.map(x => ({
           value: x.dealerCode,
           text: x.dealerName
         }));

         localStorage.setItem('dealerList', JSON.stringify(tempList));

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = tempList;
             columns.find(item => item.dataIndex === fieldName).filterItems = tempList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = tempList;
           });
         }
       }
     },

     /*
      * 获取所有经销商
      *   columns 要搜索的数组
      *   dataField 匹配的属性
      *   type form  表单 table 表格
      * */
     async getAllDelears2() {
       let res = await byAllTranslate();
       let tempList = [];
       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;

         tempList = dataList.map(x => ({
           value: x.dealerCode,
           text: x.dealerName
         }));
       }
       return tempList;
     },

     /*
      * 获取所有用户
      *   columns 要搜索的数组
      *   dataField 匹配的属性
      *   type form  表单 table 表格
      * */
     async getAllUsers(columns, dataField, type = 'table') {
       let fieldList = [];
       if (!Array.isArray(dataField)) {
         fieldList = [dataField];
       } else {
         fieldList = dataField;
       }

       let res = await getAllUser();

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;

         //去除重复数据
         dataList = arrayObjDeWeight(dataList, 'userId');

         let tempList = dataList.map(x => ({
           value: String(x.userId),
           text: String(x.employeeName)
         }));

         localStorage.setItem('allUsers', JSON.stringify(tempList));

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = tempList;
             columns.find(item => item.dataIndex === fieldName).filterItems = tempList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = tempList;
           });
         }
       }
     },

     /*
      * 获取所有用户
      *   columns 要搜索的数组
      *   userIds:请求参数 id组成的字符串
      *   dataField 匹配的属性
      *   type form  表单 table 表格
      * */
     async getNameByIds(columns, userIds, dataField, type = 'table') {
       let fieldList = [];
       if (!Array.isArray(dataField)) {
         fieldList = [dataField];
       } else {
         fieldList = dataField;
       }

       let res = await getNameByIds({ userIds: userIds });

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;
         let tempList = dataList.map(x => ({
           value: String(x.employeeId),
           text: String(x.employeeName)
         }));

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = tempList;
             columns.find(item => item.dataIndex === fieldName).filterItems = tempList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = tempList;
           });
         }
       }
     },

     // 品牌
     async getBrand(columns, dataField, type = 'table') {
       let fieldList = [];
       if (!Array.isArray(dataField)) {
         fieldList = [dataField];
       } else {
         fieldList = dataField;
       }

       const res = await getAllBrand();

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;

         let tempList = dataList.map(x => ({
           value: x.brandId + '',
           text: x.brandName
         }));

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = tempList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = tempList;
           });
         }
       }
     },

     // 所有车系
     async getSeries(columns, dataField, type = 'table', brandId) {
       let fieldList = [];
       if (!Array.isArray(dataField)) {
         fieldList = [dataField];
       } else {
         fieldList = dataField;
       }

       const res = await getAllSeries();

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;

         let tempList = dataList.map(x => ({
           value: x.seriesId + '',
           text: x.seriesName
         }));

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = tempList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = tempList;
           });
         }
       }
     },

     // 车型
     async getAllModels(columns, dataField, type = 'table') {
       let fieldList = [];
       if (!Array.isArray(dataField)) {
         fieldList = [dataField];
       } else {
         fieldList = dataField;
       }

       const res = await getAllModel();

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;

         let tempList = dataList.map(x => ({
           value: x.modelId + '',
           text: x.modelName
         }));

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = tempList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = tempList;
           });
         }
       }
     },

     async getDeptList(columns, dataField, type = 'table') {
       const res = await getOrgList();

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;

         let deptList = dataList.map(x => ({
           text: x.orgName,
           value: x.orgId
         }));

         let fieldList = [];
         if (!Array.isArray(dataField)) {
           fieldList = [dataField];
         } else {
           fieldList = dataField;
         }

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = columns.find(item => item.dataIndex === fieldName).filterItems = deptList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = deptList;
           });
         }
       }
     },

     async getProvine(columns, dataField, type = 'table') {
       const res = await getRegion();

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;

         let regionList = dataList.map(x => ({ value: x.regionId, text: x.regionName }));

         let fieldList = [];
         if (!Array.isArray(dataField)) {
           fieldList = [dataField];
         } else {
           fieldList = dataField;
         }

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = regionList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = regionList;
           });
         }
       }
     },

     /**
      * 翻译单位字典码
      * @param code
      * @returns {*}
      */
     async getAllUnitCode(columns, dataField, type = 'table') {
       let fieldList = [];
       if (!Array.isArray(dataField)) {
         fieldList = [dataField];
       } else {
         fieldList = dataField;
       }

       let res = await queryUnitCode();

       if (res.resultCode == 200 && res.success == true) {
         let dataList = isEmpty(res.data) ? [] : res.data;

         let tempList = dataList.map(x => ({
           value: String(x.UNIT_CODE),
           text: String(x.UNIT_NAME)
         }));

         console.log('tempUrl---', tempList);

         if (type == 'table') {
           fieldList.forEach(fieldName => {
             columns.find(item => item.dataIndex === fieldName).dictItems = tempList;
             columns.find(item => item.dataIndex === fieldName).filterItems = tempList;
           });
         } else {
           fieldList.forEach(fieldName => {
             columns.find(item => item.fieldName === fieldName).itemList = tempList;
           });
         }
       }
     },

     // val -> 数据的值    code -> 数据字典的 code 码
     createDealerText(code) {
       let data = isEmpty(localStorage.getItem('dealerList')) ? null : localStorage.getItem('dealerList');
       let dealerList = JSON.parse(data);
       if (isEmpty(dealerList)) {
         return code;
       }
       if (Array.isArray(dealerList)) {
         const target = dealerList.find(x => x.value == code);

         if (isEmpty(target)) {
           return code;
         } else {
           console.log('text', target.text);
           return target['text'];
         }
       } else {
         return code;
       }
     },
     //检查时间间隔是否通过
     checkTime(start,end,month) {
       let diffMonth = moment(end).diff(moment(start), 'months');
       if (diffMonth >= month) {
         return true;
       } else {
         return false;
       }
     }
   }
 };
